<?php
/**
 * @version		$Id: servers.php 6 2011-12-20 14:04:23Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AlmHosterModelServers extends JModelList
{
 	
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'ServerId', 'a.ServerId',
				'ServerName', 'a.ServerName',
				'ServerIp', 'a.ServerIp',
				'ServerSSL', 'a.ServerSSL',
				'ServerCost', 'a.ServerCost',
				'ServerDefault', 'a.ServerDefault'
			);
		}

		parent::__construct($config);
	}
	
	protected function populateState($ordering = null, $direction = null)
	{

		$app = JFactory::getApplication('administrator');
		$pk = JRequest::getInt('ServerId');
		
		$filters = JRequest::getVar('filters');
		if (empty($filters)) {
			$data = $app->getUserState($this->context.'.data');
			$filters = $data['filters'];
		}
		else {
			$app->setUserState($this->context.'.data', array('filters'=>$filters));
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		
		parent::populateState('a.ServerId', 'asc');		
				
	}
	
	protected function getListQuery() 
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		
		
		$query->select($this->getState('list.select', 'a.*'));
		
		$query->from('#__almh_servers AS a');
		
		$search = $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'ServerName:') === 0) {
				$query->where('a.ServerName = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(a.ServerName LIKE '.$search.')');
			}
		}
		
		$query->order($db->getEscaped($this->getState('list.ordering', 'a.ServerId')).' '.$db->getEscaped($this->getState('list.direction', 'asc')));
			
		return $query;		
	}	
		
	public function _ImportAccounts() 
	{	
	 	$db = JFactory::getDBO();
	 	$pk = JRequest::getInt('ServerId');

		$xmlresults = curldata($pk,listacc());		
		$xmldata = new SimpleXMLElement($xmlresults);
		
		foreach ($xmldata->acct as $acc){
			
			$query	= $db->getQuery(true);
			$query = "UPDATE  #__almh_whmaccounts SET WhmaccEmail='$acc->email', WhmaccPlan='$acc->plan', WhmaccStatus='$acc->suspended' WHERE WhmaccDomain='$acc->domain'";
			$db->setQuery( $query );
 			$db->query();
 			
			$query	= $db->getQuery(true);
			$query = "SELECT * FROM #__almh_whmaccounts WHERE WhmaccDomain='$acc->domain'";
			$db->setQuery($query);
			$db->query();
			$num_rows = $db->getNumRows();		
				if($num_rows == 0) { 
				$query = "INSERT INTO #__almh_whmaccounts (WhmaccServer, WhmaccUser, WhmaccEmail, WhmaccIp, WhmaccDomain, WhmaccPlan, WhmaccStatus) VALUES 
 				('$pk', '$acc->user', '$acc->email', '$acc->ip', '$acc->domain', '$acc->plan', '$acc->suspended')";
 				$db->setQuery( $query );
 				$db->query();	
			}
		}		
		return true;	
	}
}